package com.starsoft.frame.base.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.bean.BaseUser;
import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.core.orm.PageObject;

@Repository
public class AcountDao extends BaseDao<BaseAcount, Integer>{
	
	public List<BaseAcount> getAcount(BaseUser user){
		Map<String, Object> params = new HashMap<>();
		params.put("userId", user.getUserId());
		return this.findByMap(params);
	}

	public List<BaseAcount> getSunAcounts(BaseAcount acount){
		String hql = "from BaseAcount where userId=?0 and parentId=?1";
		return find(hql, acount.getUserId(), acount.getAcountId());
	}

	public List<BaseAcount> getParentAcount(Integer userId) {
		String hql = "from BaseAcount where userId=?0 and parentId is null";
		return find(hql, userId);
	}

	public Map<String, Object> pageParentAcount(BaseUser user, PageObject po) {
		String hql = "from BaseAcount where userId=?0 and parentId is null";
		return pageQuery(hql, po, user.getUserId());
	}
	
	public BaseAcount insertDefaultAcount(BaseUser user){
		BaseAcount acount = new BaseAcount();
		acount.setUserId(user.getUserId());
		acount.setAcountDesc("系统自动创建默认账户");
		acount.setAcountName("默认账户");
		this.save(acount);
		user.setDefautAcount(acount.getAcountId());
		this.saveOrUpdate(user);
		return acount;
	}
	
}
